package com.jixiantubu.wode;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.example.jixiantubu.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jixiantubu.adapter.Wo_shoucangTieziAdapter;
import com.jixiantubu.bean.PostInfoBean;
import com.jixiantubu.luntan.Tiezi_Info_Activity;
import com.jixiantubu.util.MyApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ShoucangTieziActivity extends Activity{

	ListView listview;
	List<PostInfoBean> list=new ArrayList<PostInfoBean>();
	HttpUtils httpUtils=new HttpUtils();
	Wo_shoucangTieziAdapter adapter;
	MyApplication application;
	ImageView shezhifanhui;
	
	
	Intent intent;
	String username;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		window.requestFeature(window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_shoucang_tiezi);
		intent = getIntent();
		username = intent.getStringExtra("username");
		Log.e("ShoucangTieziActivity------->", username);
		initViews();
		initData();
		
		
	}
	private void initViews() {
		shezhifanhui = (ImageView) findViewById(R.id.wo_shezhi);
		listview=(ListView) findViewById(R.id.wo_shoucangtiezi_lv);
		
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
				Intent intent=new Intent(ShoucangTieziActivity.this,Tiezi_Info_Activity.class);
				intent.putExtra("position", list.get(arg2));
				startActivity(intent);
			}
		});
		
	}
	private void initData() {
		adapter=new Wo_shoucangTieziAdapter(ShoucangTieziActivity.this,list);
		listview.setAdapter(adapter);
		String url = "http://" + MyApplication.getIP()
				+ ":8080/Jinnang/Wo_Post_servlet";
		RequestParams requParams = new RequestParams();

		requParams.addBodyParameter("key", "3");
		requParams.addBodyParameter("username", username);
		Log.e("key3---------", username);

		httpUtils.send(HttpMethod.POST, url, requParams,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						String result = arg0.result;
						// shiyongGson框架进行json解析
						Gson gson = new Gson();
						Type typeOfT = new TypeToken<List<PostInfoBean>>() {
						}.getType();
						List<PostInfoBean> list1 = gson.fromJson(result,
								typeOfT);
						list.addAll(list1);
						Log.e("hua-eee-----", list.toString());
						adapter.notifyDataSetChanged();

					}
				});

	}
	public void fanhui(View v) {
		finish();
	}


}
